//
//  HZMainTabBarController.m
//  WangYiNews
//
//  Created by WangPengHua on 16/1/10.
//  Copyright © 2016年 PengHua Wang. All rights reserved.
//

#import "HZMainTabBarController.h"
#import "HZNavigationController.h"
#import "HZBottomTabBar.h"
@interface HZMainTabBarController () <HZBottomTabBarDelegate>
/** tabBar的名称*/
@property (nonatomic, strong) NSArray *tabBarName;
@end

@implementation HZMainTabBarController
#pragma mark - 懒加载
- (NSArray *)tabBarName{
    if (_tabBarName == nil) {
        _tabBarName = @[@"新闻", @"阅读", @"视听", @"发现", @"我"];
    }
    return _tabBarName;
}
#pragma mark - 生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化子控制器
    [self setupChildViewControllers];
    
    [self setCustomTabBar];
}
- (void)setupChildViewControllers{
    
    HZNavigationController *navMainHall = [self navigationControllerWithStoryBoardName:@"Main"];
    
    HZNavigationController *navReadingHall = [self navigationControllerWithStoryBoardName:@"Reading"];
    
    HZNavigationController *navVideoHall = [self navigationControllerWithStoryBoardName:@"Video"];
    
    HZNavigationController *navDiscoverHall = [self navigationControllerWithStoryBoardName:@"Discover"];
   
    HZNavigationController *navMeHall = [self navigationControllerWithStoryBoardName:@"Me"];
    
    self.viewControllers = @[navMainHall, navReadingHall, navVideoHall, navDiscoverHall, navMeHall];
}
/**
 * 从stroybord中加载视图
 */
- (HZNavigationController *)navigationControllerWithStoryBoardName:(NSString *)name{
    //1.加载storyboard中的文件
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:name bundle:nil];
    //2. 创建storyboard中箭头指向的初始化控制器
    return [storyboard instantiateInitialViewController];
}
/**
 * 设置自定义的tabBar
 */
- (void)setCustomTabBar{
    //1.创建自定义的tabBar
    HZBottomTabBar *tabBar = [[HZBottomTabBar alloc]init];
    
    //通过循环创建tabBarButton
    NSUInteger count = self.viewControllers.count;
    
    for (int i = 0; i < count; i++) {
        
        NSString *normal = [NSString stringWithFormat:@"TabBar%d",(i+1)];
        NSString *selected = [NSString stringWithFormat:@"TabBar%dSel",(i+1)];
        
        [tabBar addBottomBarButtonWithNormalImageName:normal selected:selected title:self.tabBarName[i]];
        
    }
    //设置frame并添加到系统的tabBar中
    tabBar.frame = self.tabBar.bounds;
    [self.tabBar addSubview:tabBar];
    //设置代理
    tabBar.delegate = self;
}
#pragma mark - HZBottomTabBarDelegate
- (void)bottomTabBar:(HZBottomTabBar *)tabBar didCurrentClickButtonIndex:(int)index{
    self.selectedIndex = index;
    NSLog(@"%d",index);
    NSLog(@"%lu",(unsigned long)self.selectedIndex);
}
#pragma mark - 发生内存警告调用的方法,在这个方法里面清除缓存
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
